/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pl.put.dcs.workManagerServer;

import java.util.ArrayList; 
import java.util.logging.Level;
import org.apache.log4j.Logger;
import pl.put.dcs.workManagerServer.projectManager.exceptions.ProjectErrorException;
import pl.put.dcs.workManagerServer.serviceConnectors.ControlModuleServerConnector;
import pl.put.model.project.Project;
import pl.put.dcs.workManagerServer.classLoaders.LibraryClassLoader;
import pl.put.dcs.workManagerServer.classLoaders.exceptions.LibraryClassLoaderException;
import pl.put.dcs.workManagerServer.config.ServerConfigManager;

import java.util.List;
import java.util.Timer;
import pl.put.dcs.workManagerServer.deployer.Deployer;
import pl.put.dcs.workManagerServer.deployer.listeners.DeployListener;
import pl.put.dcs.workManagerServer.projectManager.ProjectManager;
import pl.put.dcs.workManagerServer.serviceConnectors.ControlModuleServerConnectorInterface;

/**
 *
 * @author LukaszR
 */
public class WorkManager
{

    private ControlModuleServerConnectorInterface controlModuleServerConnector;

    private List<ProjectManager> projectManagerList;
    public static Logger log = Logger.getLogger( WorkManager.class.getName() );
    private String projectLibPath;
    private Deployer deployer;
    private Timer deployerTimer;

    /**
     * Laduje projekt dla classPatha
     */
    public void init()
    {
        // wczytanie konfiguracji serwera
        log.info( "init" );
        // zainicjowanie listy projectManagerow
        projectManagerList = new ArrayList< ProjectManager >();
        // pobranie referencji na singleton odnosnie connectora do modulu informacyjnego
        controlModuleServerConnector = ControlModuleServerConnector.getInstance();
        controlModuleServerConnector.connect( ServerConfigManager.getInformationManagerHost(), ServerConfigManager.getInformationManagerPort() );
        // pobranie konfiguracji
        projectLibPath = ServerConfigManager.getProjectLibPath();
        // zaladowanie bibliotek do ClassPath-a. Ladowane sa tylko biblioteki NIE jary projektow
        LibraryClassLoader classLoader = LibraryClassLoader.getInstance();
        try
        {
            LibraryClassLoader.addJar( projectLibPath );
            // inicjacja ProjectManager-ow
            initDeployer();
        }
        catch ( LibraryClassLoaderException ex )
        {
            log.error( ex );
        }
        


    }

    /**
     * Inicjalizacja Deployera.
     * @TODO DODAC TIMER!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Pierwsze tykniecie ma zaladowac wszystkie projekty.
     */
    private void initDeployer()
    {
        deployer = new Deployer();
        deployer.addDeployListener( new DeployListener()
                                        {
                                            public void newProjectAdded( Project project )
                                            {
                                                log.info( "new event from deployer" );
                                                initProjectManager( project );
                                            }
                                        });
        deployer.fullDeploy();
        deployerTimer = new Timer();
        deployerTimer.schedule( deployer, 0, 5000 );
    }

    public List<ProjectManager> getProjectManagerList () {
        return projectManagerList;
    }

    private void initProjectManager (Project project) {
        // inicjacja ProjectManager
        ProjectManager projMgr = new ProjectManager( project );
        log.info( "add project manager");
        projectManagerList.add( projMgr );
        try
        {
            projMgr.initProject();
        }
        catch ( ProjectErrorException ex )
        {
            log.error( "Init " + project.getProjectName() + " project error. Project id = " + project.getId().toString() );
            log.error( ex );
        }
    }

    private List< Project > getActiveProjectsList () {
        return getControlModuleServerConnector().getActiveProjectsList();
    }


    public ControlModuleServerConnectorInterface getControlModuleServerConnector () {
        return controlModuleServerConnector;
    }

    public void setControlModuleServerConnector (ControlModuleServerConnectorInterface val)
    {
        controlModuleServerConnector = val;
    }

    public List< Project > getStartedProjectsList () {
        List< Project > ret = new ArrayList< Project >();
        for ( ProjectManager pm : projectManagerList )
        {
            ret.add( pm.getProject() );
        }
        return ret;
    }

    public void setProjectManagerList ( List< ProjectManager > val )
    {
        projectManagerList = ( List<ProjectManager> )val;
    }
}